package com.ruike.alisurface.ui.payPage;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.ruike.alisurface.https.MchApiHttp;
import com.ruike.alisurface.R;
import com.ruike.alisurface.base.BaseAty;
import com.ruike.alisurface.bean.ShopOrderBean;
import com.ruike.alisurface.ui.ShopMain_Aty;
import com.ruike.alisurface.utils.CountTimeUtils;
import com.ruike.alisurface.utils.DialogTipUtils;
import com.ruike.alisurface.utils.GsonUtils;
import com.ruike.alisurface.utils.QrcodeUtils;
import com.voodoo.lib_http.ApiListener;
import com.voodoo.lib_logcat.L;
import com.voodoo.lib_utils.imageLoader.GlideImageLoader;

import org.json.JSONObject;

import java.util.Timer;
import java.util.TimerTask;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 扫码支付查询页
 */
public class PayQrcode_Aty extends BaseAty implements ApiListener {
    @BindView(R.id.payqr_times)
    TextView payqrTimes;
    @BindView(R.id.payqr_url_qrcode)
    ImageView payqrUrlQrcode;
    @BindView(R.id.payqr_monery_tv)
    TextView payqrMoneryTv;
    Timer paytimer;
    TimerTask timerTask;

    @Override
    protected int getLayoutResId() {
        return R.layout.app_payqrcode;
    }

    CountTimeUtils countTimeUtils;

    @Override
    protected void initViews() {
        initTitleBar(false);
        countTimeUtils = new CountTimeUtils(120) {
            @Override
            public void onTick(long mMillisUntilFinished) {
                payqrTimes.setText(String.format("%d", mMillisUntilFinished));
            }

            @Override
            public void onFinish() {
                L.i("onFinish=", "onFinish    ");
                startActivity(ShopMain_Aty.class, null);
                finish();
            }
        };
        countTimeUtils.start();
    }

    ShopOrderBean shopOrderBean;//订单信息
    String json;//接收的订单json 数据
    Bitmap mQrcode;//支付二维码的位图

    @Override
    protected void initData() {
        Bundle bundle = getIntent().getExtras();
        if (bundle != null) {
            json = getIntent().getStringExtra("shopOrderBean");
            shopOrderBean = GsonUtils.fromJson(json.toString(), ShopOrderBean.class);
            if (shopOrderBean != null) {
                mQrcode = QrcodeUtils.createQrCode(shopOrderBean.getData().getUrl());
                GlideImageLoader.getInstance().loadBitmap(this, mQrcode, payqrUrlQrcode);
                payqrMoneryTv.setText("总金额: " + shopOrderBean.getData().getPay_fee());
                L.i("订单信息==", shopOrderBean.getData().getDetails());
                startCheckPayTimer();
            }
        }
    }

    @OnClick({R.id.payqr_back})
    @Override
    public void onClick(View view) {
        super.onClick(view);
        switch (view.getId()) {
            case R.id.payqr_back:
                DialogTipUtils.showTipDialog(PayQrcode_Aty.this, new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if (DialogTipUtils.xToast != null) {
                            DialogTipUtils.xToast.cancel();
                        }
                        startActivity(ShopMain_Aty.class, null);
                        finish();
                    }
                });
                break;

        }

    }

    public void mCancelTime() {
        if (countTimeUtils != null) {
            countTimeUtils.cancel();
            countTimeUtils = null;
        }
    }

    private void startCheckPayTimer() {
        setCanal();
        paytimer = new Timer();
        timerTask = new TimerTask() {
            @Override
            public void run() {
                L.i("查询支付结果");
                MchApiHttp.getPayResult(PayQrcode_Aty.this, shopOrderBean.getData().getId());
            }
        };
        paytimer.schedule(timerTask, 3000);
    }

    @Override
    protected void onPause() {
        super.onPause();
        mCancelTime();
        setCanal();
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();

        //释放数据
        shopOrderBean = null;
        json = null;
        if (mQrcode != null) {
            mQrcode.recycle();
            mQrcode = null;
        }

    }

    public void setCanal() {
        if (timerTask != null) {
            L.i("==setCanal=取消计时=timerTask=" + timerTask);
            timerTask.cancel();
            timerTask = null;
        }
        if (paytimer != null) {
            L.i("==setCanal=取消计时=paytimer" + paytimer);
            paytimer.cancel();
            paytimer.purge();
            paytimer = null;
        }
    }

    @Override
    public void onRequestStart(String requestUrl, int requestId) {

    }

    @Override
    public void onRequestComplete(String requestUrl, int requestId) {

    }

    @Override
    public void onSuccess(String requestUrl, JSONObject requestJson, int requestId) {
        if (requestUrl.endsWith("getPayResult")) {
            setCanal();
            L.i("payresult=", "支付结果==" + requestJson.toString());
            doStartActivity(PayShopShipment_Aty.class, json);
        }
    }


    int mErrorCount;

    @Override
    public void onError(String requestUrl, String errorMsg, String resultData, int requestId) {
        if (requestUrl.endsWith("getPayResult")) {
            mErrorCount++;
            L.i("payresult=", "查询次数==" + mErrorCount + "支付结果==" + errorMsg);
            startCheckPayTimer();
           /*  if (mErrorCount > 2) {
                setCanal();
                doStartActivity(PayShopShipment_Aty.class, json);
            }*/
        }
    }
}
